package com.longcai.controller.web;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.longcai.bean.ApiResult;
import com.longcai.entity.customer.ClyjUser;
import com.longcai.entity.customer.customerVo.ClyjFuyouSmallcompanyRegistervo;
import com.longcai.entity.order.ClyjGoodsOrderDetails;
import com.longcai.feign.client.WebFeignClient;
import com.longcai.service.ClyjGoodsOrderDetailsService;
import com.longcai.util.StringUtils;
import com.longcai.util.TokenUtils;
import com.longcai.vo.ClyjGoodsOrderDetailsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 商品订单表 前端控制器
 * </p>
 *
 * @author ZJ
 * @since 2019-04-19
 */
@RestController
@RequestMapping("/clyjGoodsOrderDetails/")
public class ClyjGoodsOrderDetailsController {

    @Autowired
    private ClyjGoodsOrderDetailsService clyjGoodsOrderDetailsService;
    @Autowired
    private WebFeignClient webFeignClient;
    @Autowired
    private TokenUtils tokenUtils;

    /**
     * 创建订单
     *
     * @param clyjGoodsOrderDetailsVo
     * @return
     */
    @PostMapping(value = "createOrder")
    public ApiResult<?> createOrder(@RequestBody ClyjGoodsOrderDetailsVo clyjGoodsOrderDetailsVo) {
        if (StringUtils.isEmpty(clyjGoodsOrderDetailsVo.getToken())) {
            return ApiResult.fail("您好，您的账号未登录请重新登录！");
        }
        ClyjUser clyjUser = tokenUtils.Itoken(clyjGoodsOrderDetailsVo.getToken());
        if (clyjUser == null) {
            return ApiResult.fail("您好，你的账号已登陆失效，请重新登陆！");
        }

        List<ClyjGoodsOrderDetails> list = clyjGoodsOrderDetailsVo.getClyjGoodsOrderDetails();
        if (list == null || list.size() == 0) {
            return ApiResult.fail("错误的订单参数");
        }
        for (ClyjGoodsOrderDetails clyjGoodsOrderDetails : list) {
            boolean exit = (clyjGoodsOrderDetails.getPayAmount() == null || clyjGoodsOrderDetails.getPayAmount().equals(BigDecimal.ZERO)) ||
                    clyjGoodsOrderDetails.getOrderType() == null ||
                    (clyjGoodsOrderDetails.getGoodsAmount() == null || clyjGoodsOrderDetails.getGoodsAmount() == 0) ||
                    clyjGoodsOrderDetails.getReceiver() == null || clyjGoodsOrderDetails.getReceiverAddress() == null ||
                    clyjGoodsOrderDetails.getReceiverPhone() == null || StringUtils.isEmpty(clyjGoodsOrderDetails.getShopsId()) ||
                    StringUtils.isEmpty(clyjGoodsOrderDetails.getShopsName());
            if (exit) {
                return ApiResult.fail("错误的订单参数");
            }
        }
        // 订单校验
        ApiResult<?> result = this.webFeignClient.testOrder(clyjGoodsOrderDetailsVo);
        if ("fail".equals(result.getCode())) {
            return ApiResult.fail(result.getMsg());
        }

        // 获取分润人
        String listTxt = JSONArray.toJSONString(result.getData());
        List<ClyjFuyouSmallcompanyRegistervo> touches = JSONArray.parseArray(listTxt, ClyjFuyouSmallcompanyRegistervo.class);

        return this.clyjGoodsOrderDetailsService.createOrder(clyjUser, clyjGoodsOrderDetailsVo, touches);
    }

    /**
     * 根据用户id查询订单
     *
     * @param params
     * @return
     */
    @PostMapping(value = "selectOrder")
    public ApiResult<?> selectOrder(@RequestBody JSONObject params) {
        String token = params.getString("token");
        if (token == null || "".equals(token)) {
            return ApiResult.fail("您好，你的账号已登陆失效，请重新登陆！");
        }
        ClyjUser clyjUser = tokenUtils.Itoken(token);
        if (clyjUser == null) {
            return ApiResult.fail("您好，你的账号已登陆失效，请重新登陆！");
        }
        List<ClyjGoodsOrderDetails> list = clyjGoodsOrderDetailsService.selectByUserId(clyjUser.getUserId());
        return ApiResult.success(list);
    }

    /**
     * 取消订单
     *
     * @param params
     * @return
     */
    @PostMapping(value = "cancelOrder")
    public ApiResult<?> cancelOrder(@RequestBody JSONObject params) {
        String token = params.getString("token");
        if (token == null || "".equals(token)) {
            return ApiResult.fail("您好，你的账号已登陆失效，请重新登陆！");
        }
        ClyjUser clyjUser = tokenUtils.Itoken(token);
        if (clyjUser == null) {
            return ApiResult.fail("您好，你的账号已登陆失效，请重新登陆！");
        }
        String trade_no = params.getString("trade_no");
        if (StringUtils.isEmpty(trade_no)) {
            return ApiResult.fail("错误的订单单号！");
        }
        return this.clyjGoodsOrderDetailsService.cancelOrder(trade_no);
    }
}